//
//  WFRouter.m
//  WisdomFarm
//
//  Created by sky on 2017/10/29.
//  Copyright © 2017年 sky. All rights reserved.
//

#import "WFRouter.h"
#import "WFLoginViewController.h"
#import "WFMyFarmListViewController.h"
#import "WFUserEditViewController.h"
#import "WFAgricultureUploadViewController.h"
#import "WFDisasterUploadViewController.h"

@implementation WFRouter

+ (void)toLogin {
    WFLoginViewController *controller = [[WFLoginViewController alloc] initWithStyle:UITableViewStylePlain];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
    [[UIApplication sharedApplication] sc_presentViewController:nav animated:YES completion:nil];
}

+ (void)toLoginFinished:(void (^)(BOOL))completion {
    WFLoginViewController *controller = [[WFLoginViewController alloc] initWithStyle:UITableViewStylePlain];
    controller.loginFinished = completion;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
    [[UIApplication sharedApplication] sc_presentViewController:nav animated:YES completion:nil];
}

+ (void)loginIfNeededWithFinishHandle:(void (^)(BOOL))completion {
    if ([WFStore currentUser]) {
        completion(YES);
        return;
    }
    @weakify(self);
    WFLoginViewController *controller = [[WFLoginViewController alloc] initWithStyle:UITableViewStylePlain];
    controller.loginFinished = ^(BOOL result) {
        @strongify(self);
        [controller dismissViewControllerAnimated:YES completion:^{
            completion(result);
        }];
    };
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
    [[UIApplication sharedApplication] sc_presentViewController:nav animated:YES completion:nil];
}

+ (void)toFarmListWithParams:(NSDictionary *)params {
    [self loginIfNeededWithFinishHandle:^(BOOL result) {
        if (result) {
            WFMyFarmListViewController *controller = [WFMyFarmListViewController controllerWithParams:params];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
            [[UIApplication sharedApplication] sc_presentViewController:nav animated:YES completion:nil];
        }
    }];
}

+ (void)toUserEdit {
    [WFRouter loginIfNeededWithFinishHandle:^(BOOL result) {
        if (result) {
            WFUserEditViewController *controller = [[WFUserEditViewController alloc] init];
            [[UIApplication sharedApplication] sc_pushViewController:controller animated:YES];
        }
    }];
}

+ (void)toAgricultureUpload {
    WFAgricultureUploadViewController *controller = [WFAgricultureUploadViewController new];
    [[UIApplication sharedApplication] sc_pushViewController:controller animated:YES];
}

+ (void)toDisasterUpload {
    WFDisasterUploadViewController *controller = [WFDisasterUploadViewController new];
    [[UIApplication sharedApplication] sc_pushViewController:controller animated:YES];
}

@end
